package com.demo.javaee;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
public class ThisEscapeExample {
    private int num = 8;

    /**
     * 不要在构造函数中启动线程
     */
    public ThisEscapeExample() throws InterruptedException {
        // System.out.println(this.num);
        new Thread(new EscapeRunnable()).start();
        TimeUnit.MILLISECONDS.sleep(200);
    }

    private class EscapeRunnable implements Runnable {
        @Override
        public void run() {
            // 通过ThisEscape.this就可以引用外围类对象, 但是此时外围类对象可能还没有构造完成, 即发生了外围类的this引用的逃逸
            System.out.println(ThisEscapeExample.this.num);
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        new ThisEscapeExample();
        System.out.println("end");
        System.in.read();
    }
}
